<?php
require_once 'config21.php';
if (!isset($_SESSION['username'])) {
    header('Location: login21.php');
    exit();
}

if (isset($_GET['id'])) {
    $student_id = $_GET['id'];
    $sql = "SELECT * FROM students WHERE student_id = $student_id";
    $result = $conn->query($sql);
    if ($result->num_rows == 1) {
        $row = $result->fetch_assoc();
    } else {
        echo "未找到该学生信息";
        exit;
    }
} else {
    echo "缺少学生 ID";
    exit;
}

// 获取所有班级信息用于下拉选择
$class_sql = "SELECT * FROM classes";
$class_result = $conn->query($class_sql);

// 修改学生
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['edit_student'])) {
    $student_name = $_POST['student_name'];
    $gender = $_POST['gender'];
    $birth_date = $_POST['birth_date'];
    $contact_info = $_POST['contact_info'];
    $class_id = $_POST['class_id'];
    
    $sql = "UPDATE students SET 
            student_name = '$student_name', 
            gender = '$gender', 
            birth_date = '$birth_date', 
            contact_info = '$contact_info', 
            class_id = $class_id 
            WHERE student_id = $student_id";
            
    if ($conn->query($sql)) {
        header('Location: student_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改学生信息</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h2>修改学生信息</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?id=$student_id" ?>" method="post">
        <label for="student_name">姓名:</label>
        <input type="text" name="student_name" value="<?php echo $row['student_name']; ?>" required><br>
        
        <label for="gender">性别:</label>
        <select name="gender" required>
            <option value="男" <?php if($row['gender'] == '男') echo 'selected'; ?>>男</option>
            <option value="女" <?php if($row['gender'] == '女') echo 'selected'; ?>>女</option>
        </select><br>
        
        <label for="birth_date">出生日期:</label>
        <input type="date" name="birth_date" value="<?php echo $row['birth_date']; ?>" required><br>
        
        <label for="contact_info">联系方式:</label>
        <input type="text" name="contact_info" value="<?php echo $row['contact_info']; ?>"><br>
        
        <label for="class_id">所属班级:</label>
        <select name="class_id" required>
            <?php
            while ($class_row = $class_result->fetch_assoc()) {
                $selected = ($class_row['class_id'] == $row['class_id']) ? 'selected' : '';
                echo "<option value='" . $class_row['class_id'] . "' $selected>" . $class_row['class_name'] . "</option>";
            }
            ?>
        </select><br>
        
        <input type="submit" name="edit_student" value="修改">
    </form>
    <a href="student_management21.php">返回学生管理页面</a>
</body>
</html>